package com.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;

/**
 * test
 *
 * @author ch
 * @date 2024/2/19 11:06
 */
@RestController
@RequestMapping
public class TestController {

    @GetMapping("/a")
    public Mono<String> test1() {
        return Mono.create(monoSink -> new Thread(() -> {
            String a = a();
            monoSink.success(a);
        }).start());
    }

    @Autowired
    private ThreadPoolTaskExecutor threadPoolExecutor;

    @GetMapping("/b")
    public Mono<String> test2() {
        return Mono.create(monoSink -> CompletableFuture.supplyAsync(
                        this::a
                        , threadPoolExecutor)
                .whenComplete((data, e) -> monoSink.success(data)));
    }

    private String a() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "a";
    }

}
